<template>
    <div id="monthWorkList">
      <el-dialog title="项目信息" :visible.sync="dialogFormVisible">
        <el-form :model="form" :rules="rules" ref="form" >
          <el-form-item label="项目名称" prop="name" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="项目编码" prop="code"  :label-width="formLabelWidth">
            <el-col :span="12">
              <el-input v-model="form.code" :disabled="codeBtype" autocomplete="off"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="备注" prop="remark" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-input v-model="form.remark" autocomplete="off"></el-input>
            </el-col>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="formSubmit('form')">确 定</el-button>
        </div>
      </el-dialog>
      <el-dialog title="分配人员" :visible.sync="dialogAllotVisible">
        <el-select v-model="valuePeople" multiple placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogAllotVisible = false">取 消</el-button>
          <el-button type="primary" @click="allotSubmit">确 定</el-button>
        </div>
      </el-dialog>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="项目名称">
          <el-input v-model="formInline.name" placeholder="项目名称"></el-input>
        </el-form-item>
        <el-form-item label="项目编码">
          <el-input v-model="formInline.code" placeholder="项目编码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="success" @click="add">新增</el-button>
        </el-form-item>
      </el-form>
      <el-table
          :data="tableData"
          border
          style="width: 100%">
          <el-table-column
            prop="pCreateTime"
            label="创建时间"
            align="center"
            width="">
          </el-table-column>
          <el-table-column
            prop="pName"
            label="项目名称"
            align="center"
            width="">
          </el-table-column>
          <el-table-column
            prop="pCode"
            label="项目编码"
            align="center">
          </el-table-column>
          <el-table-column
            prop="pNode"
            label="备注"
            align="center">
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            align="center"
            width="300">
            <template slot-scope="scope">
              <el-button @click="handleClick(scope.row)" type="text" size="small">修改</el-button>
              <el-button @click.native.prevent="deleteRow(scope.$index, tableData)" type="text" size="small">删除</el-button>
              <el-button @click="handleAllot(scope.row)" type="text" size="small">分配人员</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[15, 25, 50]"
            :page-size="perPage"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
        <el-input v-model="operaType" type="hidden"></el-input>
        <el-input v-model="rowId" type="hidden"></el-input>
    </div>
</template>
<script>
export default {
  name: 'monthWorkList',
  data () {
    return {
      formInline: {
        name: '',
        code: ''
      },
      rules: {
        name: [
          { required: true, message: '请填写项目名', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请填写项目编码', trigger: 'blur' }
        ]
        // ,
        // remark: [
        //   { required: true, message: '请填写备注', trigger: 'blur' }
        // ]
      },
      operaType:'',
      rowId:'',
      codeBtype:false,
      valuePeople: [],
      options: [],
      form: {
        name: '',
        code: '',
        remark: ''
      },
      formLabelWidth: '320px',
      dialogFormVisible: false,
      dialogAllotVisible: false,
      currentPage: 1,
      perPage: 15,
      //总页数
      total: null,
      tableData: []
    };
  },
  created() {
    //调用加载表格方法
    this.getTableList();
  },
  beforeDestroy() {},
  computed: {},
  mounted() {},
  methods: {
    //条件检索查询
    onSubmit() {
      //console.log('submit!');
      var self = this;
      self.getTableList();
    },
    //新增
    add() {
      var self = this;
      self.dialogFormVisible = true;
      self.operaType = 'add';
      self.codeBtype = false;
      self.form.name = '';
      self.form.code = '';
      self.form.remark = '';
    },
    //分配人员
    handleAllot(row){ 
      var self = this;
      self.dialogAllotVisible = true;
      self.rowId = row.id;
      
      //查询分配人员
      self.
        postRequest("/project/isAllotUsers", {
          id:self.rowId
          // userIds:self.valuePeople
        }).then(
          res => {      
            console.log(res.data) 
            console.log(res.data.userIds) 
            self.valuePeople = res.data.userIds;
            var obj = res.data.userEntities;
            //self.options = res.data.data;
            var arr = [];
            //var row = {};
            for(var i in obj) {
              var row = {};
              row.label = obj[i].name;
              row.value = obj[i].id;
              arr.push(row);
            }
            //console.log(arr)
            self.options = arr;

          }
        );
    },
    //选择分配人员确定
    allotSubmit() {
      var self = this;
      console.log(typeof self.valuePeople)
      console.log(self.valuePeople)
      if(self.valuePeople == null || self.valuePeople == ''){
        this.$message({
            message: '提交不能为空',
            type: 'error'
        });
        return false;
      }
      self.
        postRequest("/project/allotUsers", {
          id:self.rowId,
          userIds:self.valuePeople
        }).then(
          res => {      
            //console.log(res) 
            if(res.data.result == '1' ){            
              this.$message({
                  message: res.data.message,
                  type: 'success'
              });
            }else if(res.data.result == '2' ){
              this.$message({
                  message: res.data.message,
                  type: 'error'
              });
            }   
            this.dialogAllotVisible = false;
            self.getTableList();
          }
        );
    },
    //确定新增项目
    formSubmit(formName) {
      //console.log('submit!');      
      this.$refs[formName].validate((valid) => {
          if (valid) {
            var self = this;
            console.log(self.operaType)
            if(self.operaType == 'add'){
              self.
                postRequest("/project/insert", {
                  pName:self.form.name,
                  pCode:self.form.code,
                  pNode:self.form.remark
                }).then(
                  res => {      
                    console.log(res) 
                    if(res.data.result == '1' ){            
                      this.$message({
                          message: res.data.message,
                          type: 'success'
                      });
                    }else if(res.data.result == '2' ){
                      this.$message({
                          message: res.data.message,
                          type: 'error'
                      });
                    }else if(res.data.result == '-2' ){
                      this.$message({
                          message: res.data.message,
                          type: 'error'
                      });
                    }    
                    this.dialogFormVisible = false;
                    self.getTableList();
                  }
                );
            }else if(self.operaType == 'update'){
              //console.log(self.rowId)
              self.
                postRequest("/project/update", {
                  pName:self.form.name,
                  id:self.rowId,
                  pNode:self.form.remark
                }).then(
                  res => {      
                    //console.log(res) 
                    if(res.data.result == '1' ){            
                      this.$message({
                          message: res.data.message,
                          type: 'success'
                      });
                      self.getTableList();
                    }else if(res.data.result == '2' ){
                      this.$message({
                          message: res.data.message,
                          type: 'error'
                      });
                    }else if(res.data.result == '-2' ){
                      this.$message({
                          message: res.data.message,
                          type: 'error'
                      });
                    }    
                    this.dialogFormVisible = false;
                    self.getTableList();
                    self.codeBtype = false;
                  }
                );
            }
          }
      })
      
      
    },
    //修改
    handleClick(row) {
      //console.log(row);
      var self = this;
      self.rowId = row.id;
      self.operaType = 'update';
      self.codeBtype = true;
      self.dialogFormVisible = true;
      //console.log(thselfis.form)
      self.form.name = row.pName;
      self.form.code = row.pCode;
      self.form.remark = row.pNode;
      
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      var self = this;
      self.perPage = val;
      self.getTableList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      var self = this;
      self.currentPage = val;
      self.getTableList();
    },
    deleteRow(index, rows) {
      console.log(rows[index])
      var self = this;
      self.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          self.
            postRequest("/project/delete", {
              id:rows[index].id
            }).then(
              res => {      
                console.log(res.data) 
                if(res.data.result == '1' ){            
                  this.$message({
                      message: res.data.message,
                      type: 'success'
                  });
                }else if(res.data.result == '2' ){
                  this.$message({
                      message: res.data.message,
                      type: 'error'
                  });
                }  
                self.getTableList();  
              }
            );
            
        }).catch(() => {
          self.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    getTableList() {
      var self = this;
      //console.log(self.currentPage)
      self.getRequest("/project/queryByPage?page_no=" +self.currentPage+'&&page_size='+self.perPage
      +'&&projectName='+self.formInline.name+'&&projectCode='+self.formInline.code
      , {}).then(
        res => {      
          console.log(res.data)     
          self.tableData = res.data.data;
          self.total = res.data.recordsTotal;
          self.perPage = res.data.length;
        }
      );
    }
  }
}
</script>

<style scoped>
  .demo-form-inline {text-align: left;}
  .el-pagination {margin: 20px 0;}
</style>